from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    openai_api_base="https://api.siliconflow.cn/v1/",
    openai_api_key="sk-pdfifkpjdlxvyvgkerbluaotktpznsmpbcvskjauotenxgvz",  # app_key
    model_name="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",  # 模型名称
)
chunks = []
# for chunk in llm.stream("天空是什么颜色的"):
#     print(chunk)
#     chunks.append(chunk)
#     print(chunk.content,end="|",flush=True)

print("ok")

###异步调用
import asyncio

prompt = ChatPromptTemplate.from_template("你是谁")
parser = StrOutputParser()
chain = prompt | llm | parser
async def async_stream():
    # pass
    events = []
    # async for event in chain.astream_events({"topic":"程序员"},version='v2'):
    async for event in llm.astream_events("你是谁",version='v2'):
        events.append(event)
    print(events)

asyncio.run(async_stream())